﻿using Flattiverse;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FlattiverseGame
{
    public partial class GameUI : Form
    {
        Connector connector;

        GameField gameField;
        ChooseUniverseControl chooseUniverseControl;

        List<IDrawable> drawList = new List<IDrawable>();

        public GameUI()
        {
            InitializeComponent();

            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);

            try
            {
                connector = new Connector("ddraghici@gmx.de", "6g']u0JA");

                chooseUniverseControl = new ChooseUniverseControl(connector);
                chooseUniverseControl.Finished += chooseUniverseControl_Finished;

                chooseUniverseControl.Dock = DockStyle.Fill;
                this.Controls.Add(chooseUniverseControl);

                gameField = new GameField(connector);
            }
            catch (Exception e) 
            { 
                Console.WriteLine("Verbindungsfehler");
                Console.WriteLine(e.Message);
            }
        }

        void chooseUniverseControl_Finished(object sender, EventArgs e)
        {
            if (chooseUniverseControl.UniverseGroup != null && chooseUniverseControl.Team != null)
            {
                gameField.InitField(chooseUniverseControl.UniverseGroup, chooseUniverseControl.Team, "dannyd");

                gameField.Dock = DockStyle.Fill;
                this.Controls.Add(gameField);
                this.Controls.Remove(chooseUniverseControl);

                chooseUniverseControl.Dispose();

                drawTimer.Start();
            }
            else
                Console.WriteLine("Something went wrong");
        } 

        private void GameUI_FormClosed(object sender, FormClosedEventArgs e)
        {
            
        }

        private void drawTimer_Tick(object sender, EventArgs e)
        {
            if (gameField != null && gameField.IsInit)
                gameField.Draw();
        }

        private void GameUI_Paint(object sender, PaintEventArgs e)
        {
            
        }

        private void GameUI_KeyDown(object sender, KeyEventArgs e)
        {
               
        }

        private void GameUI_FormClosing(object sender, FormClosingEventArgs e)
        {
            drawTimer.Stop();

            if (gameField != null && gameField.IsInit)
                gameField.Close();

            connector.Close();
        }

        
    }
}
